﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using LisenceUtils.Enums;
using LisenceUtils.Windows;

namespace LisenceUtils
{
    public class Main
    {
        private static readonly IHardwareInfoHandle HardwareInfoHandle = LisenceUtils.HardwareInfoHandle.Create();
        private MyLicense _lic;
        private string _msg = string.Empty;
        private LicenseStatus _status = LicenseStatus.Undefined;
        public byte[] CertPubicKeyData;

        public Main()
        {
            MyLicense.Default.GetUidEvent += OnGetUidEvent;
        }

        public string DateFormat { get; set; }

        public string DateTimeFormat { get; set; }

        private string OnGetUidEvent()
        {
            return HardwareInfoHandle.GenerateUid(ConstInfo.AppName);
        }

        public bool ActivateLicense()
        {
            if (File.Exists(ConstInfo.LicenseFile))
            {
                _lic = MyLicense.Default.ParseLicenseFromBase64String(File.ReadAllText(ConstInfo.LicenseFile),
                    CertPubicKeyData) as MyLicense;
                _status = MyLicense.Default.LicenseStatus;
            }
            else
            {
                _status = LicenseStatus.Invalid;
                _msg = "应用没有被激活，请激活后使用";
            }
            switch (_status)
            {
                case LicenseStatus.Valid:

                    ShowLicenseInfo(_lic);
                    break;
                default:
                    MessageBox.Show(_msg, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    var activate = new LincenseActivate();
                    activate.AppName = ConstInfo.AppName;
                    activate.CertificatePublicKeyData = CertPubicKeyData;
                    activate.TxtUid.Text = HardwareInfoHandle.GenerateUid(ConstInfo.AppName);
                    var dialog = activate.ShowDialog();
                    var showDialog = dialog != null && (bool) dialog;
                    if (showDialog)
                    {
                        MessageBox.Show("验证成功请重新启动", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    return false;
            }

            return true;
        }

        public void ShowLicenseInfo(LicenseEntity license)
        {
            try
            {
                var sb = new StringBuilder(512);
                var typeLic = license.GetType();
                var props = typeLic.GetProperties();

                var formatedValue = string.Empty;
                foreach (var p in props)
                {
                    try
                    {
                        var showInfo =
                            (ShowInLicenseInfoAttribute)
                                Attribute.GetCustomAttribute(p, typeof (ShowInLicenseInfoAttribute));
                        if (showInfo != null && showInfo.ShowInLicenseInfo)
                        {
                            var value = p.GetValue(license, null);
                            sb.Append(showInfo.DisplayAs);
                            sb.Append(": ");
                            if (value != null)
                            {
                                switch (showInfo.DataFormatType)
                                {
                                    case ShowInLicenseInfoAttribute.FormatType.String:
                                        formatedValue = "已认证";
                                        break;
                                    case ShowInLicenseInfoAttribute.FormatType.DateTime:
                                        if (p.PropertyType == typeof (DateTime) &&
                                            !string.IsNullOrWhiteSpace(DateTimeFormat))
                                        {
                                            formatedValue = ((DateTime) value).ToString(DateTimeFormat);
                                        }
                                        break;
                                    case ShowInLicenseInfoAttribute.FormatType.EnumDescription:
                                        formatedValue = Enum.GetName(p.PropertyType, value);
                                        break;
                                }

                                sb.Append(formatedValue);
                            }

                            sb.Append("\r\n");
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
                var licenseInfo = new LicenseInfo();
                licenseInfo.SetInfo(sb.ToString());
                licenseInfo.ShowDialog();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
    }
}